package com.mp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mp.core.utils.DiyBeanUtils;
import com.mp.core.utils.Utils;
import com.mp.dao.WebsiteUrlMapper;
import com.mp.dao.WebsiteUserMapper;
import com.mp.pojo.Query.WebsiteUrlQuery;
import com.mp.pojo.Query.WebsiteUserQuery;
import com.mp.pojo.VO.WebsiteUrlVO;
import com.mp.pojo.WebsiteUrl;
import com.mp.pojo.WebsiteUser;
import com.mp.service.WebsiteUrlService;
import com.mp.service.WebsiteUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author jack
 * @data 2019-08-08 16:35
 */
@Service
public class WebsiteUserServiceImpl extends ServiceImpl<WebsiteUserMapper, WebsiteUser> implements WebsiteUserService {

    @Autowired
    private WebsiteUserMapper websiteUserMapper;

    @Autowired
    private WebsiteUrlMapper websiteUrlMapper;


    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public String saveOrUpdate(@Valid WebsiteUserQuery websiteUserQuery) {
        //站点 新增 或者修改
        WebsiteUser websiteUser = new WebsiteUser();
        DiyBeanUtils.copyProperties(websiteUserQuery, websiteUser);
        //1.1 adminId，websiteId 查到用户只能修改
        WebsiteUser selectWebsiteUser = new LambdaQueryChainWrapper<>(websiteUserMapper)
                .eq(WebsiteUser::getWebsiteId, websiteUserQuery.getWebsiteId())
                .eq(WebsiteUser::getAdminId, websiteUserQuery.getAdminId()).one();
        if (selectWebsiteUser != null) {
            websiteUser.setId(selectWebsiteUser.getId());
            websiteUserMapper.updateById(websiteUser);
        }else {
            websiteUserMapper.insert(websiteUser);
        }
        //2.1获取网址list
        List<WebsiteUrlQuery> websiteUrlQueryList = websiteUserQuery.getWebsiteUrlList();
        //2.2 删除已存在的网址， 新增新网址
        new LambdaUpdateChainWrapper<>(websiteUrlMapper)
                .eq(WebsiteUrl::getWebsiteUserId, websiteUser.getId()).remove();
        if (websiteUrlQueryList != null || websiteUrlQueryList.size() > 0) {
            websiteUrlQueryList.forEach(wu -> {
                if (!Utils.isNull(new Object[] { wu.getName(), wu.getUrl()})){
                    websiteUrlMapper.insert(new WebsiteUrl(wu.getName(), wu.getUrl(), websiteUser.getId()));

                }
            });
        }
        return "";
    }
}
